package ord2.search;

import java.util.Arrays;
import java.util.LinkedList;
import ord2.core.Parameter;

/**
 * This class implements <code>ParametricSearcher</code> interface.
 * It's a abstract searcher that provides parametric searching and creation.
 * @see ParametricSearcher
 *
 * @author José Ivan
 * @author Edviges
 *
 */
public abstract class AbstractParametricSearcher<K, T> implements ParametricSearcher<K, T> {

    private LinkedList<Parameter> pars = new LinkedList<Parameter>();

    /**
     * Creates a searcher using especified parameters
     * @param pars Array with Parameter
     * @throws NullPointerException if pars is null
     * @see ord2.core.Parameter
     */
    public AbstractParametricSearcher(Parameter[] pars) {
    
        if (pars == null) {
            
            throw new NullPointerException("Parameters array cannot be null");
        }
        
        this.pars.addAll(Arrays.asList(pars));
    }
    
    /**
     * Creates a searcher using default parameters.
     * @see ord2.core.Parameter
     */
    public AbstractParametricSearcher() {
        
        //Nothing
    }
    
    @Override
    public Parameter[] getParameters() {
        
        return this.pars.toArray(new Parameter[0]);
    }

    /**
     * Adds a parameter to be used by this searcher.
     * @param newParameter Parameter to be added
     * @throws NullPointerException if newParameter is null
     */
    @Override
    public void addParameter(Parameter newParameter) {
        
        if (newParameter == null) {
            
            throw new NullPointerException("Parameter to be added cannot be null");
        }
        
        this.pars.add(newParameter);
    }

    /**
     * Removes given parameter from this searcher
     * @param par Parameter to be removed
     */
    @Override
    public void removeParameter(Parameter par) {
        
        if (par == null) {
            
            throw new NullPointerException("Parameter to be removed cannot be null");
        }
        
        this.pars.remove(par);
    }
    
}
